﻿using System;
using System.IO;
using System.Text;
using CID.Domain.Infrastructure;
using CID.Execution.Agent.TemplateHandling;
using CID.Execution.Support.Information;
using NUnit.Framework;

namespace CID.Execution.Agent.Tests.TemplateHandling
{
    [TestFixture]
    class BATTemplateFileWriterTests : BaseAgentTests
    {
        private const string TemplateContentFilePath = @".\Artifacts\ValidBATTemplate.bat";
        private static readonly ParameterValueInfo StringParameterValue = new ParameterValueInfo() { ParameterName = "StringParameter", ParameterType = ParameterType.String, StringValue = "Some string value" };
        private static readonly ParameterValueInfo IntegerParameterValue = new ParameterValueInfo() { ParameterName = "IntegerParameter", ParameterType = ParameterType.Integer, IntegerValue = 123 };
        private static readonly ParameterValueInfo BooleanParameterValue = new ParameterValueInfo() { ParameterName = "BooleanParameter", ParameterType = ParameterType.Boolean, BooleanValue = false };
        private static readonly ParameterValueInfo DecimalParameterValue = new ParameterValueInfo() { ParameterName = "DecimalParameter", ParameterType = ParameterType.Decimal, DecimalValue = 1.23m };
        private static readonly ParameterValueInfo DateTimeParameterValue = new ParameterValueInfo() { ParameterName = "DateTimeParameter", ParameterType = ParameterType.DateTime,  DateTimeValue = DateTime.Today };
        private static readonly ParameterValueInfo UniqueIdentifierParameterValue = new ParameterValueInfo() { ParameterName = "UniqueIdentifierParameter", ParameterType = ParameterType.UniqueIdentifier, UniqueIdentifierValue = Guid.NewGuid() };
        private static readonly ParameterValueInfo XmlParameterValue = new ParameterValueInfo() { ParameterName = "XmlParameter", ParameterType = ParameterType.Xml, XmlValue = "<root><someNode attribute='on'/></root>" };

        private byte[] templateContent;
        private string outputFilePath;
        private ParameterValueInfo[] parameterValues;

        public override void SetUp()
        {
            base.SetUp();

            templateContent = File.ReadAllBytes(TemplateContentFilePath);
            outputFilePath = Path.GetTempFileName();
            parameterValues = new ParameterValueInfo[] { StringParameterValue, IntegerParameterValue, BooleanParameterValue, 
                DecimalParameterValue, DateTimeParameterValue, UniqueIdentifierParameterValue, XmlParameterValue };
        }

        public override void TearDown()
        {
            base.TearDown();

            if (File.Exists(outputFilePath)) File.Delete(outputFilePath);
        }

        [Test]
        public void WriteTo_ShouldCreateOutputFileWithGivenData()
        {
            var writer = new BATTemplateFileWriter(templateContent, parameterValues);

            writer.WriteTo(outputFilePath);

            Assert.IsTrue(File.Exists(outputFilePath));
            var outputFileContent = File.ReadAllText(outputFilePath, Encoding.ASCII);
            var templateFileContent = File.ReadAllText(TemplateContentFilePath, Encoding.ASCII);
            StringAssert.Contains("echo off", outputFileContent);
            StringAssert.Contains("echo on", outputFileContent);
            StringAssert.Contains(templateFileContent, outputFileContent);
            StringAssert.Contains(String.Format("SET {0}=", StringParameterValue.ParameterName), outputFileContent);
            StringAssert.Contains(String.Format("SET {0}=", IntegerParameterValue.ParameterName), outputFileContent);
            StringAssert.Contains(String.Format("SET {0}=", DecimalParameterValue.ParameterName), outputFileContent);
            StringAssert.Contains(String.Format("SET {0}=", DateTimeParameterValue.ParameterName), outputFileContent);
            StringAssert.Contains(String.Format("SET {0}=", BooleanParameterValue.ParameterName), outputFileContent);
            StringAssert.Contains(String.Format("SET {0}=", UniqueIdentifierParameterValue.ParameterName), outputFileContent);
            StringAssert.Contains(String.Format("SET {0}=", XmlParameterValue.ParameterName), outputFileContent);
            StringAssert.Contains(StringParameterValue.StringValue, outputFileContent);
            StringAssert.Contains(ParameterValueSerializer.Serialize(IntegerParameterValue.IntegerValue.Value), outputFileContent);
            StringAssert.Contains(ParameterValueSerializer.Serialize(DecimalParameterValue.DecimalValue.Value), outputFileContent);
            StringAssert.Contains(ParameterValueSerializer.Serialize(DateTimeParameterValue.DateTimeValue.Value), outputFileContent);
            StringAssert.Contains(ParameterValueSerializer.Serialize(BooleanParameterValue.BooleanValue.Value), outputFileContent);
            StringAssert.Contains(ParameterValueSerializer.Serialize(UniqueIdentifierParameterValue.UniqueIdentifierValue.Value), outputFileContent);
            StringAssert.Contains(XmlParameterValue.XmlValue, outputFileContent);

        }
    }
}
